﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Model;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Converters;
namespace TaoBaoProject.Handler.RegisterHandler
{
  /// <summary>
  /// GetRegisterHandler 的摘要说明
  /// </summary>
  public class GetRegisterHandler : IHttpHandler
  {

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";

      DataTable dt;
      string isSearch = context.Request.Params["isSearch"] == null ? "" : context.Request.Params["isSearch"].ToString();

      SearchPara sp = new SearchPara();
      sp.haschecked = context.Request.Params["hasChecked"] == null ? "" : context.Request.Params["hasChecked"].ToString();

      sp.chkqq = context.Request.Params["chkqq"] == null ? "" : context.Request.Params["chkqq"].ToString();
      sp.keywords = context.Request.Params["keywords"] == null ? "" : context.Request.Params["keywords"].ToString();
      sp.datefrom = context.Request.Params["datefrom"] == null ? "" : context.Request.Params["datefrom"].ToString();
      sp.dateto = context.Request.Params["dateto"] == null ? "" : context.Request.Params["dateto"].ToString();
      sp.pricefrom = context.Request.Params["pricefrom"] == null ? "" : context.Request.Params["pricefrom"].ToString();
      sp.priceto = context.Request.Params["priceto"] == null ? "" : context.Request.Params["priceto"].ToString();
      sp.chkqq = context.Request.Params["chkqq"] == null ? "" : context.Request.Params["chkqq"].ToString();
      sp.chbcaifutong = context.Request.Params["chbcaifutong"] == null ? "" : context.Request.Params["chbcaifutong"].ToString();
      sp.chbcaifutongname = context.Request.Params["chbcaifutongname"] == null ? "" : context.Request.Params["chbcaifutongname"].ToString();
      sp.chbzhifubaoname = context.Request.Params["chbzhifubaoname"] == null ? "" : context.Request.Params["chbzhifubaoname"].ToString();
      sp.chbzhifubaoaccount = context.Request.Params["chbzhifubaoaccount"] == null ? "" : context.Request.Params["chbzhifubaoaccount"].ToString();
      sp.chbfkzhifubaoname = context.Request.Params["chbfkzhifubaoname"] == null ? "" : context.Request.Params["chbfkzhifubaoname"].ToString();
      sp.chbfkzhifubao = context.Request.Params["chbfkzhifubao"] == null ? "" : context.Request.Params["chbfkzhifubao"].ToString();
      sp.limitdays = string.IsNullOrEmpty(context.Request.Params["limitdays"]) ? 0 : Convert.ToInt32(context.Request.Params["limitdays"].ToString());

      sp.userName = context.Request.Params["userName"] == null ? "" : context.Request.Params["userName"].ToString();
      sp.shopId = context.Request.Params["shopId"] == null ? "" : context.Request.Params["shopId"].ToString();

      sp.searchScope = context.Request.Params["searchScope"] == null ? "" : context.Request.Params["searchScope"].ToString();
      sp.isSourceQuery = context.Request.Params["isSourceQuery"] == null ? "" : context.Request.Params["isSourceQuery"].ToString();
      sp.isDirectAddRegisterFlag = context.Request.Params["isDirectAddRegisterFlag"] == null ? "" : context.Request.Params["isDirectAddRegisterFlag"].ToString();


      int ItemTotalCount = 0;
      int pagesize = GetPageSize(context);
      int page = GetPageIndex(context);
      DataSet ds;


      //如果在直接点击“新增登记” 这个时候是不需要 进行搜索
      //如果后期定和需要改。。那只需要判断sp.isDirectAddRegisterFlag 这个字段就ok了~~。
      if (isSearch != string.Empty)
      {
        //为什么为什么为什么。。为什么在条件为空的时候使用“GetAllRegisterBySearchPara” 这个那么的慢呢？？
        if (sp.keywords == "" && sp.shopId == "100000" && sp.userName == "选择用户" && sp.pricefrom == "" && sp.priceto == "" && sp.limitdays == 0)
        {
          ds = RegisterBLL.GetAllRegisterByPagination("ProjectFileds", "Shops", "*", "1=1", "Date", pagesize, page, out ItemTotalCount);
          dt = ds.Tables[0];
        }
        else
        {
          dt = RegisterBLL.GetAllRegisterBySearchPara(sp, pagesize, page, out ItemTotalCount);
        }
      }
      else
      {
        ds = RegisterBLL.GetAllRegisterByPagination("ProjectFileds", "Shops", "*", "1=1", "Date", pagesize, page, out ItemTotalCount);
        dt = ds.Tables[0];
      }

      //-----------------------------------2014.07.24   start.
      //if (sp.limitdays > 0)
      //{
      //  dt = RegisterBLL.GetAllRegisterBySearchPara(sp, pagesize, page, out ItemTotalCount);
      //}
      ////-----------------------------------2014.07.24  end

      //***********Handler Shop Name*************
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        if (dt.Rows[i]["ShopColor"].ToString().Trim() != "")
        {
          dt.Rows[i]["ShopName"] = dt.Rows[i]["ShopName"] + "," + dt.Rows[i]["ShopColor"];
        }
      }
      //***********Handler Shop Name*************

      double totalCash = 0.0;
      string cashStr = "";
      if (sp.keywords == "")
      {
        if ((sp.userName != "" && sp.userName != "选择用户") || (sp.shopId != "" && sp.shopId != "100000"))
        {
          DataTable dtMoney = RegisterBLL.GetAllMoney(sp);
          for (int i = 0; i < dtMoney.Rows.Count; i++)
          {
            double temp = 0.0;
            bool isOK = double.TryParse(dtMoney.Rows[i]["Cash"].ToString(), out temp);
            if (isOK)
              totalCash += temp;
          }
          cashStr = "金额：" + totalCash.ToString() + " 元(￥)";
        }
      }

      JsonSerializerSettings setting = new JsonSerializerSettings();
      IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
      iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

      setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
      string result = JsonConvert.SerializeObject(new { total = ItemTotalCount, rows = dt, totalMoney = cashStr }, Formatting.Indented, iso);
      context.Response.Write(result);
    }
    public Int32 GetPageSize(HttpContext context)
    {
      try
      {
        return Int32.Parse(context.Request["rows"].ToString());
      }
      catch
      {
        return 10;
      }
    }

    public Int32 GetPageIndex(HttpContext context)
    {
      try
      {
        return context.Request["page"].ToString() == "0" ? 1 : Int32.Parse(context.Request["page"].ToString());
        //return Int32.Parse(context.Request["page"].ToString());
      }
      catch
      {
        return 1;
      }
    }


    public bool IsReusable
    {
      get
      {
        return false;
      }
    }


  }
  public class employee
  {
    public string id { get; set; }
    public string name { get; set; }
  }
}